package pkg.andru.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Locale;

import android.graphics.Paint;
import android.util.Log;
import android.view.View;

/**
 * <b>some super tools, use the reflect to implements.</br> take care use of
 * these methods.</b>
 * 
 * @author idiottiger
 * @version 1.0
 */
public final class SuperUtils {

    static final String LOG_TAG = "SuperUtils";

    static final String GET_SYS_PROP_ERROR = "get sys prop error:";

    /**
     * get system properties, return <b>int</b>
     * 
     * @param name
     *            prop name
     * @param def
     *            default value if dont have the prop
     * @return
     */
    public static int getIntSysProp(String name, int def) {
        Class<?> sProperties = null;
        int prop = def;
        try {
            sProperties = Class.forName("android.os.SystemProperties");
            prop = (Integer) (sProperties.getMethod("get", String.class, int.class).invoke(null, name, def));
        } catch (Exception e) {
            Log.e(LOG_TAG, GET_SYS_PROP_ERROR, e);
        }
        return prop;
    }

    /**
     * get system properties, return <b>long</b>
     * 
     * @param name
     *            prop name
     * @param def
     *            default value if dont have the prop
     * @return
     */
    public static long getLongSysProp(String name, long def) {
        Class<?> sProperties = null;
        long prop = def;
        try {
            sProperties = Class.forName("android.os.SystemProperties");
            prop = (Long) (sProperties.getMethod("get", String.class, long.class).invoke(null, name, def));
        } catch (Exception e) {
            Log.e(LOG_TAG, GET_SYS_PROP_ERROR, e);
        }
        return prop;
    }

    /**
     * get system properties, return <b>boolean</b>
     * 
     * @param name
     *            prop name
     * @param def
     *            default value if dont have the prop
     * @return
     */
    public static boolean getBooleanSysProp(String name, boolean def) {
        Class<?> sProperties = null;
        boolean prop = def;
        try {
            sProperties = Class.forName("android.os.SystemProperties");
            prop = (Boolean) (sProperties.getMethod("get", String.class, boolean.class).invoke(null, name, def));
        } catch (Exception e) {
            Log.e(LOG_TAG, GET_SYS_PROP_ERROR, e);
        }
        return prop;
    }

    /**
     * get system properties, return <b>String</b>
     * 
     * @param name
     *            prop name
     * @param def
     *            default value if dont have the prop
     * @return
     */
    public static String getSysProp(String name, String def) {
        Class<?> sProperties = null;
        String prop = def;
        try {
            sProperties = Class.forName("android.os.SystemProperties");
            prop = (String) (sProperties.getMethod("get", String.class, String.class).invoke(null, name, def));
        } catch (Exception e) {
            Log.e(LOG_TAG, GET_SYS_PROP_ERROR, e);
        }
        return prop;
    }

    /**
     * disable hardware accelerated
     * 
     * @param view
     */
    public static void disableHardwareAccelerated(View view) {
        try {
            Field layerTypeFiled = View.class.getDeclaredField("LAYER_TYPE_SOFTWARE");
            if (layerTypeFiled != null) {
                final int LAYER_TYPE_SOFTWARE = layerTypeFiled.getInt(view);
                Method method = View.class.getMethod("setLayerType", int.class, Paint.class);
                if (method != null) {
                    method.invoke(view, LAYER_TYPE_SOFTWARE, null);
                    Log.w(LOG_TAG, "finish disableHardwareAccelerated LAYER_TYPE_SOFTWARE:" + LAYER_TYPE_SOFTWARE);
                }
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "disableHardwareAccelerated error:" + e.getMessage());
        }

    }

    /**
     * change the locale
     * 
     * @param locale
     *            change the locale to this
     */
    public static void changeLocale(Locale locale) {
        try {
            Class<?> activityManagerNative = Class.forName("android.app.ActivityManagerNative");

            Object am = activityManagerNative.getMethod("getDefault").invoke(activityManagerNative);

            Object config = am.getClass().getMethod("getConfiguration").invoke(am);
            config.getClass().getDeclaredField("locale").set(config, locale);
            config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);

            am.getClass().getMethod("updateConfiguration", android.content.res.Configuration.class).invoke(am, config);
            Log.i(LOG_TAG, "send change locale request");
        } catch (Exception e) {
            Log.e(LOG_TAG, "change locale error:", e);
        }
    }

}
